#!/bin/sh
set -u
set -x

[ ! -d /root/.ethereum/geth/chaindata ] && geth init /config/genesis.json
[ $RPC ] && RPCOPT="--authrpc.vhosts=* --authrpc.addr=0.0.0.0 --authrpc.port=8551"
[ $WS ] && WSOPT="--ws --ws.addr=0.0.0.0 --ws.port=8546 --ws.origins=* --ws.api=eth,txpool,debug,web3,net,personal,admin,miner"
[ $HTTP ] && HTTPOPT="--http --http.addr=0.0.0.0 --http.corsdomain=* --http.port=8545 --http.api=eth,txpool,debug,web3,net,personal,engine,admin,miner"
[ $SINGER ] && MINE="--mine --miner.threads=1 --miner.gasprice=1000000000 --miner.etherbase=0x$ACCOUNT --unlock=0x$ACCOUNT --password=/config/password"
[ $NETWORKID ] && NETWORK="--networkid=$NETWORKID"

ENODE=`bootnode --nodekey=/bootnode_keys/boot.key -writeaddress`
BOOTNODEIP=`getent hosts bootnode | awk '{ print $1 }'`
BOOTNODE="--bootnodes enode://$ENODE@[$BOOTNODEIP]:30301"

KEYSTORE=/root/.ethereum/
cp -r /config/keystore $KEYSTORE

geth \
    $RPCOPT \
    $WSOPT \
    $HTTPOPT \
    $MINE \
    $NETWORK \
    $BOOTNODE \
    --allow-insecure-unlock \
    --port=30301 \
    --verbosity $LOGGING_LEVEL
